//添加机务设施数据库
var addJwsssjk = function(){
    var contentDiv = function(){
        var content;
        var data={
            "title":{
                "type":"机务设施数据库类型",
                "city":"城市",
                "railway":"路局",
                "facilities_name":"机务设施名称",
                "nature":"性质",
                "scale":"规模",
                "longitude":"经度",
                "dimensionality":"纬度"
            },"text":{
                "type":"",
                "city":"",
                "railway":"",
                "facilities_name":"",
                "nature":"",
                "scale":"",
                "longitude":"",
                "dimensionality":""
            },"input":{
                "type":"select",
                "city":"text",
                "railway":"text",
                "facilities_name":"text",
                "nature":"text",
                "scale":"text",
                "longitude":"text",
                "dimensionality":"text"
            },"input_init":{
            }
        };
        var getDiv =  dc_update.innerHTML;
        laytpl(getDiv).render(data, function(html){
            content = html;
        });
        return content;
    }
    layopen(contentDiv());
    initType();
}

var getFileNameById = function(file_id){
	var file_name = "";
    $.ajax({
		   url:base+"/datacenter/common/findFileInfoById/"+file_id,
		   data:{},
		   async:false,
		   success:function(data,status){
			   file_name = data.caption;
		   }
	});
    return file_name;
}

//设施详细信息
var facilitiesInfo = function(obj){
	//上传CAD
    var contentDiv = function(){
        var content;
        var data={
            "title":{
                "jyjlt":"既有交路图",
                "jyjlt_show":"既有交路图",
                "jyjlt_file":"文件上传",
                "jyllt":"既有列流图",
                "jyllt_show":"既有列流图",
                "jyllt_file":"文件上传"
            },"text":{
            	"jyjlt":obj.facility_info.split(",")[0],
                "jyjlt_show":getFileNameById(obj.facility_info.split(",")[0]),
                "jyjlt_file":"",
                "jyllt":obj.facility_info.split(",")[1],
                "jyllt_show":getFileNameById(obj.facility_info.split(",")[1]),
                "jyllt_file":""
            },"input":{
            	"jyjlt":"hidden",
                "jyjlt_show":"text_readonly",
                "jyjlt_file":"upload_file",
                "jyllt":"hidden",
                "jyllt_show":"text_readonly",
                "jyllt_file":"upload_file"
            },"input_init":{
            }
        };
        var getDiv =  dc_update.innerHTML;
        laytpl(getDiv).render(data, function(html){
            content = html;
        });
        return content;
    }
    layer.open({
        title:'更新机务设施数据库'
        ,content: contentDiv()
        ,area: ['700px', '700px']
        ,scrollbar: false
        ,btn: ['确认', '取消']
        ,yes: function(index,layero){ //确认
            var data = $(layero).find("form").serializeJSON();
            var files = data.jyjlt+","+data.jyllt;
            $.ajax({
 			   url:base+"/projectCenter/businessdata/updateFacilityInfo",
 			   data:{facility_info:files,id:obj.id},
 			   success:function(data,status){
 				    if(data.statusCode == 0){
 						layer.msg(data.message, {
 							icon : 1
 						});
 				    }else{
 						layer.msg(data.message, {
 							icon : 5
 						});
 				    }
 				    initData(1);
 			   }
 		   });
        }
    });
    upload();
	
}

$("body").delegate(".layui-upload-file","click",function(){
	var data_id = $(this).attr("data_id");
	upload(data_id);
}); 

var upload = function(file_name){
	layui.upload({
		  url: base+'/datacenter/common/uploadFile?file_kind=jwsssjk'
		  ,type:'file'
		  ,ext:'dwg'
		  ,success: function(res){
			  if(res.code == '0'){
				  var original_file_name = res.original_file_name;
				  var upload_file_name = res.upload_file_name;
				  var file_id = res.file_id;
				  $("input[name='"+file_name.substring(0,file_name.indexOf("_"))+"']").val(file_id);
				  $("input[name='"+file_name.substring(0,file_name.indexOf("_"))+"_show']").val(upload_file_name);
			  }
		  }
	});
}


var updateObj = function(obj){
    var contentDiv = function(){
        var content;
        var data={
            "title":{
            	"id":"id",
                "type":"机务设施数据库类型",
                "city":"城市",
                "railway":"路局",
                "facilities_name":"机务设施名称",
                "nature":"性质",
                "scale":"规模",
                "longitude":"经度",
                "dimensionality":"纬度"
            },"text":{
            	"id":obj.id,
                "type":obj.type,
                "city":obj.city,
                "railway":obj.railway,
                "facilities_name":obj.facilities_name,
                "nature":obj.nature,
                "scale":obj.scale,
                "longitude":obj.longitude,
                "dimensionality":obj.dimensionality
            },"input":{
            	"id":"hidden",
                "type":"hidden",
                "city":"text",
                "railway":"text",
                "facilities_name":"text",
                "nature":"text",
                "scale":"text",
                "longitude":"text",
                "dimensionality":"text"
            },"input_init":{
            }
        };
        var getDiv =  dc_update.innerHTML;
        laytpl(getDiv).render(data, function(html){
            content = html;
        });
        return content;
    }
    layopen(contentDiv());
}

var initType = function(){
	$("select[name='type']").html("<option value='非车站'>非车站</option>" +
			"<option value='车站'>车站</option>");
	form.render();
}


//查看地图标记
var showMap = function(obj){
	var x_coord = obj.longitude;
	var y_coord = obj.dimensionality;
    layer.open({
    	  type: 2,
    	  title: '地图显示',
    	  shadeClose: true,
    	  shade: 0.8,
    	  area: ['800px', '600px'],
    	  content: base + "/projectCenter/businessdata/showMapForBulletFacility?x_coord="+x_coord+"&y_coord="+y_coord //iframe的url
    	}); 
	return false;
}


var deleteObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   $.ajax({
			   url:base+"/projectCenter/businessdata/delFacility/"+obj.id,
			   data:{},
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(1);
			   }
		   });
	   });
}

//打开弹窗
var layopen = function(content){
    layer.open({
        title:'更新机务设施数据库'
        ,content: content
        ,area: ['700px', '700px']
        ,scrollbar: false
        ,btn: ['确认', '取消']
        ,yes: function(index,layero){ //确认
            var data = $(layero).find("form").serializeJSON();
            $.post(base+"/projectCenter/businessdata/updateFacility",data,
                    function (data,status){
                        if(data!=""&&data!=null){
                            if(data.statusCode == 0){
                                layer.msg('更新成功', {icon: 1,time:2000});
                            }else{
                                layer.msg(data.message, {icon: 2,time:2000});
                            }
                        }else{
                            layer.msg('更新失败!', {icon: 2,time:2000});
                        }
                        initData(1);
                    })
        }
    });
}

var bindChange = function(){
	$("#type").change(function(){
		type = $(this).children("option:selected").val();
		initData(1);
	})
	
}

var type = "";

var initData = function(num){
    var param = {
            page_num:num,
            page_size:"10",
            type:type
        }
        $.ajax({
            url: base + "/projectCenter/businessdata/findCarmaintenanceFacility",
            data: param,
            success: function (data, textStatus, jqXHR) {
                if(data.list != null){
                    var pageCount = data.pager.pageCount;
                    var dataObj ={
                        "list":data.list,
                        "dataTool":{
                            "title":{
                            	"type":"机务设施数据库类型"
                                ,"city":"城市"
                                ,"railway":"路局"
                                ,"facilities_name":"机务设施名称"
                                ,"nature":"性质"
                                ,"scale":"规模"
                                ,"longitude":"经度"
                                ,"dimensionality":"纬度"
                                ,"operation":"操作"
                            },
                            "titleCheckbox":false,
                            "btn":[{"event":"updateObj","name":"编辑"},
                                   {"event":"showMap","name":"查看地图标记"},
                                   {"event":"facilitiesInfo","name":"设施详细信息"},
                                   {"event":"deleteObj","name":"删除"}]
                        }
                    };
                    showPageTable('view', dataObj,num,pageCount,"initData");
                }

            },
            error: function () {
            }
        });
}

$(document).ready(function(){
	initData(1);
	bindChange();
	$("#addJwsssjk").click(addJwsssjk);
});